package com.may.wind.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.may.wind.enums.ResultEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;
import java.util.Map;

/**
 * 统一返回结果
 * 内置静态方法，返回对象；
 * 为便于自定义统一结果的信息，建议使用链式编程，将返回对象设类本身，即return this;
 * 响应数据由于为json格式，可定义为JsonObject或Map形式
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@ApiModel(description = "返回类")
public class Result<Entity> {
    @ApiModelProperty(name = "success", value = "是否访问成功")
    private Boolean success;

    @ApiModelProperty(name = "code", value = "状态码")
    private Integer code;

    @ApiModelProperty(name = "message", value = "返回消息")
    private String message;

    @ApiModelProperty(name = "data", value = "返回数据")
    private Map<String, Entity> data = new HashMap<>();

    /**
     * 只可以调用统一返回类的方法，不可以直接创建，因此构造器私有
     */
    private Result() {
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setData(Map<String, Entity> data) {
        this.data = data;
    }

    public Boolean getSuccess() {
        return success;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, Entity> getData() {
        return data;
    }

    /**
     * 通用返回成功
     */
    public static Result ok() {
        Result result = new Result();
        result.setSuccess(ResultEnum.SUCCESS.getSuccess());
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMessage());
        return result;
    }

    /**
     * 通用返回失败，未知错误
     *
     * @return
     */
    public static Result error() {
        Result result = new Result();
        result.setSuccess(ResultEnum.UNKNOWN_ERROR.getSuccess());
        result.setCode(ResultEnum.UNKNOWN_ERROR.getCode());
        result.setMessage(ResultEnum.UNKNOWN_ERROR.getMessage());
        return result;
    }

    /**
     * 设置结果，形参为结果枚举
     *
     * @param result
     * @return
     */
    public static Result setResult(ResultEnum result) {
        Result entity = new Result();
        entity.setSuccess(result.getSuccess());
        entity.setCode(result.getCode());
        entity.setMessage(result.getMessage());
        return entity;
    }

    /**
     * ------------使用链式编程，返回类本身----------
     * 自定义返回数据
     *
     * @param map
     * @return
     */
    public Result data(Map<String, Entity> map) {
        this.setData(map);
        return this;
    }

    /**
     * 通用设置data
     *
     * @param key
     * @param value
     * @return
     */
    public Result data(String key, Entity value) {
        this.data.put(key, value);
        return this;
    }

    /**
     * 自定义状态信息
     *
     * @param message
     * @return
     */
    public Result message(String message) {
        this.setMessage(message);
        return this;
    }

    /**
     * 自定义状态码
     *
     * @param code
     * @return
     */
    public Result code(Integer code) {
        this.setCode(code);
        return this;
    }

    /**
     * 自定义返回结果
     *
     * @param success
     * @return
     */
    public Result success(Boolean success) {
        this.setSuccess(success);
        return this;
    }
}
